package com.wen.springbootorderrabbitmqproducer.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    public void makeOrder(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.err.println("订单生成成功"+orderId);
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }

    public void makeOrder1(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.err.println("订单生成成功"+orderId);
        String exchangeName = "direct_order_exchange";
        rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);

    }

    public void makeOrderTopic(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.err.println("订单生成成功"+orderId);
        String exchangeName = "topic_order_exchange";
        String routingKey = "com.email.duanxin";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }

    public void makeOrderTtl(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.err.println("订单生成成功"+orderId);
        String exchangeName = "direct_ttl_exchange";
        String routingKey = "ttl";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }
    public void makeOrderTtlMeaasge(String userId,String productId,int num){

        String orderId = UUID.randomUUID().toString();
        System.err.println("订单生成成功"+orderId);
        String exchangeName = "direct_ttl_exchange";
        String routingKey = "ttl_message";

        MessagePostProcessor postProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId,postProcessor);

    }
}
